<?php

require_once 'StarView.php';

class StarModule
{
    public $view;
    public $modx;
    public $prefix;

    public function __construct($modx, $GLOBALS = null, $a = 0, $id = 0)
    {
        $this->view = new StarView($modx, $a, $id);
        $this->modx = $modx;
        
        mysql_select_db(str_replace('`', '', $GLOBALS['dbase']));
        @mysql_query("{$GLOBALS['database_connection_method']} {$GLOBALS['database_connection_charset']}");
                 
        $this->prefix = $modx->db->config['table_prefix'];
                 
        $this->init();
    }
    
    public function init() {  }
    
    public function quote($text)
    {
        return "'".mysql_escape_string($text)."'";
    }
    
    public function dispatch($action)
    {
        $this->view->action = $action;
        $fnc = "{$action}Action";
        $this->$fnc();
        echo $this->view->render($action . '.phtml');
    }
    
    public function fetchAll($sql)
    {
        if ($res = $this->query($sql)) {
            $out = array();
            while ($row = mysql_fetch_assoc($res)) {
                $out[] = $row;
            }
            return $out;
        }
        return false;
    }
    
    public function fetchRow($sql)
    {
        if ($res = $this->query($sql)) {
            if ($row = mysql_fetch_assoc($res)) {
                return $row;
            }
        }
        return false;
    }
    
    public function fetchOne($sql)
    {
        if ($row = $this->fetchRow($sql)) {
            return reset($row);
        }
        return false;
    }
    
    public function query($sql)
    {
        if (!$res = mysql_query($sql)) {
            die("MySQL Error:<br />" . mysql_error() . "<br /><br />In query:<br />$sql");
        }
        return $res;
    }
}
